add default content type for empty requests #275
Conversation
| + resolve_default(client_meta, ()) | ||
| ) | ||
|
|
||
| request_headers.setdefault("content-type", "application/json") |
There was a problem hiding this comment.
generated apis already set content-type: application/json or x-ndjson for e.g. bulk requests before calling transport.
so setdefault is a no-op in this case, so the predefined content-type is preserved.
eg. bulk request in client where content-type is always set
https://github.com/elastic/elasticsearch-py/blob/c485ee48177892efd8083553ebf14bfaa804e59b/elasticsearch/_sync/client/__init__.py#L749-L752
e.g. where content-type is optionally set if body is present like search
https://github.com/elastic/elasticsearch-py/blob/f59037ee0e9de82719dce6ba8b56b9022d60ee0c/elasticsearch/_sync/client/watcher.py#L337-L341
|
Mostly nitpicking, but this introduces a small behavior change. Forgetting to set the content type for a request with a body would trigger an error, which is, I guess, a good thing. With this change this would not happen anymore, and the request would silently get a JSON content type. Another way to handle this without changing the original behavior would be to explicitly set the content type in the few endpoints that support not having a body. This could be done in the code generator. |
Closes elastic/elasticsearch-py#3305
Elasticsearch 9.1+ will request requests with no content-types.
Python transport layer only includes content-type when a request body is present but bodyless requests will have no content-type.
Bodyless requests will now get
application/jsonas default